package com.airsystem.pos.bengkel.controller.dialog;

import java.util.Map;
import java.util.Map.Entry;
import java.util.Vector;

import javax.swing.JOptionPane;

import org.apache.log4j.Logger;

import com.airsystem.pos.bengkel.component.TableFrame;
import com.airsystem.pos.bengkel.entity.Tagihan;
import com.airsystem.pos.bengkel.entity.loader.TagihanLoader;
import com.airsystem.pos.bengkel.entity.wrapper.TagihanWrapper;
import com.airsystem.pos.bengkel.interfaces.ICallback;
import com.airsystem.pos.bengkel.interfaces.ILoader;
import com.airsystem.pos.bengkel.interfaces.ILookup;
import com.airsystem.pos.bengkel.interfaces.ISystem;
import com.airsystem.pos.bengkel.model.dialog.MasterBillingDetailDialogModel;
import com.airsystem.pos.bengkel.util.Preconditions;

/**
 * @author Fanny Irawan Sutawanir (fannyirawans@gmail.com)
 */
public class MasterBillingDetailDialogController extends MasterBillingDetailDialogModel implements ILookup, ISystem {
	private static final Logger LOG = Logger.getLogger(MasterBillingDetailDialogController.class.getSimpleName());

	private ILoader<Map<String, TagihanWrapper>> loader;

	protected Map<String, TagihanWrapper> tagihanMap;

	public MasterBillingDetailDialogController() {
		super();

		loader = new TagihanLoader();
	}

	public void setup() {
		TableFrame.deleteTable(tableData, tableModel);
	}

	public void get() {
		String periode = tableData.getValueAt(row, 0).toString();
		tagihanRow = tagihanMap.get(periode);
	}

	public void reset() {
		searchField.setText("");
		searchCombo.requestFocus();
	}

	public void category() {
		throw new UnsupportedOperationException("Not Implemented.");
	}

	public void search() {
		try {
			Preconditions.checkEmpty(searchField);
		} catch (IllegalArgumentException e) {
			JOptionPane.showMessageDialog(this, "Silahkan input data!", "Pesan", JOptionPane.WARNING_MESSAGE);
			reset();

			return;
		}

		try {
			setup();

			((TagihanLoader) loader).setParams(getSearchData());
			loader.load(new ICallback<Map<String, TagihanWrapper>>() {
				public void get(Map<String, TagihanWrapper> result) {
					for (Entry<String, TagihanWrapper> e : result.entrySet()) {
						Vector<String> vector = new Vector<String>();
						vector.add(e.getKey());
						vector.add(e.getValue().getTotal().toString());

						tableModel.addRow(vector);
					}

					tableData.setModel(tableModel);
				}
			});
		} catch (Exception e) {
			LOG.error(e.getMessage(), e);
			JOptionPane.showMessageDialog(this, "Load data gagal!", "Pesan", JOptionPane.ERROR_MESSAGE);
		}
	}

	public void load() {
		try {
			setup();

			((TagihanLoader) loader).setParams(new Tagihan());
			loader.load(new ICallback<Map<String, TagihanWrapper>>() {
				public void get(Map<String, TagihanWrapper> result) {
					tagihanMap = result;

					for (Entry<String, TagihanWrapper> e : result.entrySet()) {
						Vector<String> vector = new Vector<String>();
						vector.add(e.getKey());
						vector.add(e.getValue().getTotal().toString());

						tableModel.addRow(vector);
					}

					tableData.setModel(tableModel);
				}
			});

			this.setVisible(true);
		} catch (Exception e) {
			this.setVisible(false);
			LOG.error(e.getMessage(), e);
			JOptionPane.showMessageDialog(this, "Load data gagal!", "Pesan", JOptionPane.ERROR_MESSAGE);
		}
	}
}